import re
from app01 import models
from django import forms
from app01.utils.bootstrap import BootstrapModelform
from django.core.exceptions import ValidationError
from app01.utils.encrypt import md5
from app01.utils.bootstrap import BootStrapForm


class user_modelform(BootstrapModelform):
    name = forms.CharField(min_length=2,
                           label='姓名',
                           widget=forms.TextInput(attrs={'class': 'form-control'}))

    class Meta:
        model = models.UserInfo
        fields = ['name', 'password', 'age', 'account', 'create_time', 'depart', 'gender']


class PrettyForm(BootstrapModelform):
    # 验证方式1：
    # mobile = forms.CharField(
    #     label='手机号',
    #     validators=[RegexValidator(r'^1[3-9]\d{9}$', '手机号格式不正确')])

    class Meta:
        model = models.PrettyMum
        fields = ['mobile', 'price', 'level', 'status']
        # fields="__all__"
        # exclude = ['level']

    # 验证方式2：
    def clean_mobile(self):
        mobile_txt = self.cleaned_data.get('mobile')
        exists = models.PrettyMum.objects.filter(mobile=mobile_txt).exists()
        if exists:
            raise forms.ValidationError('手机号已存在')
        if re.match(r'^1[3-9]\d{9}$', mobile_txt):
            return mobile_txt
        else:
            self.add_error('mobile', '手机号格式不正确')


class PrettyeditForm(BootstrapModelform):
    # mobile = forms.CharField(disabled=True, label='手机号')  # 禁止修改手机号字段

    class Meta:
        model = models.PrettyMum
        fields = ['mobile', 'price', 'level', 'status']

    def clean_mobile(self):
        mobile_txt = self.cleaned_data.get('mobile')
        # 当前编辑那一行的id
        # self.instance.pk
        exists = models.PrettyMum.objects.exclude(id=self.instance.pk).filter(mobile=mobile_txt).exists()
        if exists:
            raise forms.ValidationError('手机号已存在')
        if re.match(r'^1[3-9]\d{9}$', mobile_txt):
            return mobile_txt
        else:
            self.add_error('mobile', '手机号格式不正确')


class AdminModelForm(BootstrapModelform):
    confirm_password = forms.CharField(
        label='确认密码',
        widget=forms.PasswordInput
    )

    class Meta:
        model = models.Admin
        fields = ['username', 'password', 'confirm_password']
        widgets = {
            'password': forms.PasswordInput(render_value=True),
        }

    def clean_password(self):
        pwd = self.cleaned_data.get('password')
        return md5(pwd)

    def clean_confirm_password(self):
        pwd = self.cleaned_data.get('password')
        confirm = md5(self.cleaned_data.get('confirm_password'))
        if pwd != confirm:
            raise ValidationError('两次密码输入不一致')
        return confirm  # 返回什么，此字段以后就保存什么到数据库


class AdminEditModelForm(BootstrapModelform):
    class Meta:
        model = models.Admin
        fields = ['username']


class AdminResetModelForm(BootstrapModelform):
    confirm_password = forms.CharField(
        label='确认密码',
        widget=forms.PasswordInput(render_value=True)
    )

    class Meta:
        model = models.Admin
        fields = ['password', 'confirm_password']

    def clean_password(self):
        pwd = self.cleaned_data.get('password')
        # 去数据库校验当前密码和新输入的密码是否一致
        exists = models.Admin.objects.filter(id=self.instance.pk, password=pwd).exists()
        if exists:
            raise ValidationError('密码不能与原密码相同')
        md5_pwd = md5(pwd)
        return md5_pwd

    def clean_confirm_password(self):
        pwd = self.cleaned_data.get('password')
        confirm = md5(self.cleaned_data.get('confirm_password'))
        print(pwd, '\n', confirm)
        if pwd != confirm:
            raise ValidationError('两次密码输入不一致')
        return confirm  # 返回什么，此字段以后就保存什么到数据库


class LoginForm(BootStrapForm):
    username = forms.CharField(label='用户名',
                               widget=forms.TextInput,
                               required=True,
                               )
    password = forms.CharField(label='密码',
                               widget=forms.PasswordInput(
                                   render_value=True
                               ),
                               required=True,
                               )
    code = forms.CharField(label='验证码',
                           widget=forms.TextInput,
                           required=True,
                           )

    def clean_password(self):
        password = self.cleaned_data.get('password')
        return md5(password)


class TaskModelForm(BootstrapModelform):
    class Meta:
        model = models.Task
        fields = "__all__"
        widgets = {
            'detail': forms.TextInput,
            # 'detail': forms.Textarea,
        }


class OrderModelForm(BootstrapModelform):
    class Meta:
        model = models.Order
        exclude = ['oid', 'admin']
